package com.yunfangdata.web.controller;

import com.yunfangdata.domain.Attns;
import com.yunfangdata.query.AttnsQueryObject;
import com.yunfangdata.service.IAttnsService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("attns")
public class AttnsController {
    @Autowired
    private IAttnsService attnsService;


    @RequestMapping("")
    public String index(){
        return "attns/list";
    }
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(AttnsQueryObject qo){
        return attnsService.query(qo);
    }

  @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Attns entity) {
        JsonResult result = new JsonResult();
        try{
            attnsService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return  result;
    }

    //删除操作
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try{
            attnsService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("删除失败");
        }
        return  result;
    }






    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Attns entity) {
        JsonResult result = new JsonResult();
        try{
            attnsService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return  result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    public  JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            attnsService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult("修改错误");
        }
        return result;

    }


    //查询所有联系人
    @RequestMapping("selectByAll")
    @ResponseBody
    public List<Attns> selectByAll(){
        return attnsService.list();
    }



}